#ifndef ___EVENT__ENGINE__20140422___
#define ___EVENT__ENGINE__20140422___

#include <memory>
#include <vector>
#include <sys/types.h>
#include <functional>
#include "OSType.h"
#include "MessageEvent.h"
using namespace std;

#ifndef LINUX_HAVE_TIMERFD
class CTimeManager;
#endif

class CEvent;
class CEventEngine
{
public:
    CEventEngine();
    virtual ~CEventEngine();

public:
    Bool Initialize();

    Bool AddEvent(CEvent *lpEvent, Int32 nEventType);
    Bool ModEvent(CEvent *lpEvent, Int32 nEventType);
    Bool DelEvent(CEvent *lpEvent);

    Bool PostMessage(Int32 nMessageID, void *wParam, void *lParam, IMessageEvent *pMsgEvent);
    Bool PostMessage(Int32 nMessageID, void *wParam, void *lParam, const shared_ptr<IMessageEvent> &spCallback);
    Bool AsyncExecute(std::function<void()> fnExecutor);
    Bool AsyncExecuteAndWait(std::function<void()> fnExecutor);

    void* SetPrivatePtr(void *ptr);
    void* GetPrivatePtr() const;
    pid_t GetEngineID() const;

#ifndef LINUX_HAVE_TIMERFD
    CTimeManager* GetTimeManager();
#endif
    
public:
    virtual void Loop(Int32 nTimeout);

protected:
    void EventTest(Int32 nTimeout);

protected:
    Int32								m_nEpollFD;								// epoll 句柄
    std::vector<CEvent *>				m_vecDeleting;							// 正在回调时上层需要删除的元素
    volatile Bool						m_bOnEvent;								// 标志是否在处理 I/O 事件
    void*								m_pPrivatePtr;							// 用户私有数据

#ifndef LINUX_HAVE_EVENTFD
    CTimeManager*                       m_pTimerManager;
#endif

    pid_t                               m_uEngineID;                            // 事件线程ID
};

#endif
